package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.DestinationTheme;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.query.DestinationThemeQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationThemeService destinationThemeService;
    @Reference
    private IDestinationSiftingService destinationSiftingService;

    @RequestMapping("")
    public String index(Model model) {
        //hotRegions [{id:xx,name:xx},{id:xx,name:xx},...]
        model.addAttribute("hotRegions", regionService.listHotRegion());

        model.addAttribute("months",destinationThemeService.listMonth());

        model.addAttribute("sifting",destinationSiftingService.list());

        return "destination/index";
    }

    @RequestMapping("/guide")
    public String guide(Long id, Model model) {
        //toasts
        //dest {name:xx}
        List<Destination> toasts = destinationService.listToasts(id);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        //catalogs
        model.addAttribute("catalogs",strategyCatalogService.selectByDestId(id));
        //strategyDetails
        model.addAttribute("strategyDetails",strategyDetailService.listViewNumTop3(id));
        return "destination/guide";
    }

    @RequestMapping("/listHotDestByRegionId")
    public String listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model) {
        List<Destination> dests = destinationService.listHotDestByRegionId(regionId);

        List<Destination> leftDests = dests.subList(0, dests.size() / 2);
        List<Destination> rightDests = dests.subList(dests.size() / 2, dests.size());
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        return "destination/hotdestTpl";
    }

    //destination/listByThemeMonthId", {ThemeId:rid}
    @RequestMapping("/listByThemeMonthId")
    public String listByThemeMonthId(@ModelAttribute("ThemeId") Long ThemeId, Model model) {
        List<Destination> dests = destinationService.listByThemeId(ThemeId);

        List<Destination> upDests = dests.subList(0, dests.size() / 2);
        List<Destination> downDests = dests.subList(dests.size() / 2, dests.size());
        model.addAttribute("upDests", upDests);
        model.addAttribute("downDests", downDests);

        return "destination/monthThemeTpl";
    }

    @RequestMapping("/listThemeBySiftingId")
    public String listThemeBySiftingId(@ModelAttribute("siftingId") Long siftingId, Model model) {
        List<DestinationTheme> theme = destinationThemeService.listThemeBySiftingId(siftingId);
        model.addAttribute("theme", theme);

        return "destination/ThemeTpl";
    }

    //destination/surveyPage?destId=3261&catalogId=1
    @RequestMapping("/surveyPage")
    public String listDetailPage(@ModelAttribute("qo") DestinationQueryObject qo, Model model){
        //toasts
        List<Destination> toasts = destinationService.listToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);
        //dest {name:xx}
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));

        return "destination/survey";
    }

    //destination/survey?destId=3261&catalogId=1
    @RequestMapping("/survey")
    public String listDetail(@ModelAttribute("qo") DestinationQueryObject qo, Model model){
        List<StrategyCatalog> catalogs = strategyCatalogService.selectByDestId(qo.getDestId());
        StrategyCatalog catalog = null;
        for (StrategyCatalog strategyCatalog : catalogs) {
            if (strategyCatalog.getId().equals(qo.getCatalogId())) {
                catalog = strategyCatalog;
                break;
            }

        }
        //catalogs
        model.addAttribute("catalogs",catalogs);
        //catalog
        model.addAttribute("catalog",catalog);
        //detail
        Long detailId = catalog.getDetails().get(0).getId();
        model.addAttribute("detail",strategyDetailService.get(detailId));
        return "destination/surveyTpl";
    }
    //destination/travels
    @RequestMapping("/travels")
    public String listTravels(@ModelAttribute("qo")TravelQueryObject qo,Model model){
        //pageInfo
        PageInfo<Travel> pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }

    @RequestMapping("/destFilter")
    public String destFilter(Model model){
        model.addAttribute("month",destinationThemeService.listThemeBySiftingId(1L));//月份
        model.addAttribute("suitable",destinationThemeService.listThemeBySiftingId(2L));//全年
        model.addAttribute("season",destinationThemeService.listThemeBySiftingId(3L));//季节
        model.addAttribute("tripMode",destinationThemeService.listThemeBySiftingId(4L));//出行方式
        model.addAttribute("days",destinationThemeService.listThemeBySiftingId(5L));//出行天数
        model.addAttribute("festival",destinationThemeService.listThemeBySiftingId(6L));//节假日
        return "destination/destFilter";
    }

    @RequestMapping("/listByThemeIds")
    public Object listByThemeIds(@ModelAttribute("qo") DestinationThemeQueryObject qo, Model model){
        PageInfo<Destination> dests = destinationService.listInThemeIds(qo);
        model.addAttribute("pageInfo",dests);
        return "destination/destFilterTpl";
    }
    @RequestMapping("isFavor")
    @ResponseBody
    public Object updateIsFavor(@LoginUser UserInfo user, Long destId) {
        AjaxResult result = new AjaxResult();
        if (user == null) {
            result.mark("请先登录");
            result.setCode(102);//在这个项目中约定的,表示没有登录的错误状态码
            return result;
        }
        boolean flag = destinationService.isFavor(user, destId);
        result.setSuccess(flag);
        //result.setData();
        return result;
    }
}
